/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.server;

import cn.ac.iie.di.postbar.entity.MongoDB;
import cn.ac.iie.di.postbar.handler.bar.BarHandler;
import cn.ac.iie.di.postbar.handler.file.ParserFileHandler;
import cn.ac.iie.di.postbar.handler.mpost.MPostHandler;
import cn.ac.iie.di.postbar.handler.rpost.RPostHandler;
import cn.ac.iie.di.postbar.handler.user.UserHandler;
import cn.ac.iie.di.postbar.util.ConfigureUtil;
import cn.ac.iie.di.postbar.util.LoadTableMapData;
import cn.ac.iie.di.postbar.util.LoadNameData;
import cn.ac.iie.di.postbar.util.SearchMongodb;
import com.mongodb.client.MongoCollection;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class PostLtStartUp {

    static Logger logger = null;
    public static String nameserver = "";
    public static String bar_topic = "";
    public static String rpost_topic = "";
    public static String mpost_topic = "";
    public static String user_topic = "";
    public static String bar_dir = "";
    public static String rpost_dir = "";
    public static String mpost_dir = "";
    public static String user_dir = "";

    public static int bufSize = 0;
    public static String localpath = "";
    public static int localFileSize = 0;
    public static int coreThreadPoolSize = 0;
    public static int maxThreadPoolSize = 0;
    public static int blockingQueueSize = 0;
    public static String tablename = "";
    public static int jsonThreadSize = 0;
    public static int intervaltime = 0;
    public static int timeout = 0;
    public static ArrayBlockingQueue<String> barQueue = null;
    public static ArrayBlockingQueue<String> mpostQueue = null;
    public static ArrayBlockingQueue<String> rpostQueue = null;
    public static ArrayBlockingQueue<String> userQueue = null;
    public static String[] serverPath = new String[]{};
    public static int requestServerSize = 0;
    public static String trsclientip = "";
    public static String trsclientversion = "";
    public static HashMap<String, String> provinceMap = new HashMap<String, String>();
    public static HashMap<String, String> userplaceMap = new HashMap<String, String>();
    public static String mongo_hosts = "";
    public static String mongo_user = "";
    public static String mongo_passwd = "";
    public static String mongo_db = "";
    public static String mongo_mp_table = "";
    public static String mongo_user_table = "";
    public static MongoCollection mpMongoCollection = null;
    public static MongoCollection userMongoCollection = null;

    public static int isTianYa = 1;
    public static String yunrunURL = "";

    static {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger = Logger.getLogger(PostLtStartUp.class.getName());
    }

    public static void main(String[] args) {
        try {
            ConfigureUtil conf = new ConfigureUtil();

            isTianYa = conf.getIntProperty("isTianYa");

            nameserver = conf.getProperty("nameserver");
            logger.info("the nameserver is " + nameserver);
            mpost_topic = conf.getProperty("mpost_topic");
            logger.info("the mpost-RocketMQ:  the topic is " + mpost_topic);
            rpost_topic = conf.getProperty("rpost_topic");
            logger.info("the rpsot-RocketMQ:  the topic is " + rpost_topic);
            bar_topic = conf.getProperty("bar_topic");
            logger.info("the bar-RocketMQ:  the topic is " + bar_topic);
            user_topic = conf.getProperty("user_topic");
            logger.info("the user-RocketMQ:  the topic is " + user_topic);

            //各个通道文件夹
            mpost_dir = conf.getProperty("mpost_dir");
            rpost_dir = conf.getProperty("rpost_dir");
            bar_dir = conf.getProperty("bar_dir");
            user_dir = conf.getProperty("user_dir");
            logger.info("the mpost_dir is " + mpost_dir + " ,and the rpost_dir is " + rpost_dir + " ,and the bar_dir is " + bar_dir + " ,and the user_dir is " + user_dir);

            //ArrayBlockingQueue大小参数
            bufSize = Integer.parseInt(conf.getProperty("bufferSize"));
            //文件路径参数
            localpath = conf.getProperty("localPath");
            localFileSize = Integer.parseInt(conf.getProperty("localFileSize"));
            //线程池参数
            coreThreadPoolSize = Integer.parseInt(conf.getProperty("coreThreadPoolSize"));
            maxThreadPoolSize = Integer.parseInt(conf.getProperty("maxThreadPoolSize"));
            blockingQueueSize = Integer.parseInt(conf.getProperty("blockingQueueSize"));

            tablename = conf.getProperty("tablename");
            String serverPathStr = conf.getProperty("serverPath");
            serverPath = serverPathStr.split(";");
            requestServerSize = Integer.parseInt(conf.getProperty("requestServerSize"));

            //消费参数
            jsonThreadSize = Integer.parseInt(conf.getProperty("jsonThreadSize"));
            intervaltime = Integer.parseInt(conf.getProperty("intervaltime"));
            timeout = Integer.parseInt(conf.getProperty("timeout"));
            logger.info("the bufSize is " + bufSize + " , localpath is " + localpath + " , localFileSize is " + localFileSize
                    + " , coreThreadPoolSize is " + coreThreadPoolSize + " , maxThreadPoolSize is " + maxThreadPoolSize + " , blockingQueueSize is " + blockingQueueSize
                    + " , jsonThreadSize is " + jsonThreadSize + " , intervaltime is " + intervaltime + " , timeout is " + timeout);
            trsclientip = conf.getProperty("trsclientip");
            trsclientversion = conf.getProperty("trsclientversion");
            logger.info("the trsClient is " + trsclientip + " ,and the trsClientVersion is " + trsclientversion);

            mongo_hosts = conf.getProperty("mongo_hosts");
            mongo_user = conf.getProperty("mongo_user");
            mongo_passwd = conf.getProperty("mongo_passwd");
            mongo_db = conf.getProperty("mongo_db");
            mongo_mp_table = conf.getProperty("mongo_mp_table");
            mongo_user_table = conf.getProperty("mongo_user_table");
            logger.info("the mongoDB hosts is " + mongo_hosts + " ,and the user is " + mongo_user + " ,and the password is " + mongo_passwd + " ,and the dbname is " + mongo_db + " ,and the tablename_mp is " + mongo_mp_table + " ,and the tablename_user is " + mongo_user_table);
            yunrunURL = conf.getProperty("yunrunURL");
            logger.info("the yunrun URL is " + yunrunURL);
        } catch (Exception ex) {
            logger.error("init highlights-parser.properties failed,cause " + ex.getMessage(), ex);
            return;
        }

        mpostQueue = new ArrayBlockingQueue(bufSize);
        rpostQueue = new ArrayBlockingQueue(bufSize);
        barQueue = new ArrayBlockingQueue(bufSize);
        userQueue = new ArrayBlockingQueue(bufSize);
        logger.info("init ArrayBlockingQueue(mpostQueue,rpostQueue,barQueue,userQueue) finish, and the bufferSize is " + bufSize);

        //初始化表信息
        new LoadTableMapData().init(tablename);
        logger.info("load table finish.");

        //初始化省份信息和USERPLACE列表
        provinceMap = new LoadNameData("provincename.mapdata").init();
        userplaceMap = new LoadNameData("userplace.mapdata").init();
        logger.info("load config finish.");
        //初始化mongoDB
        mpMongoCollection = new SearchMongodb(new MongoDB(mongo_hosts, mongo_user, mongo_passwd, mongo_db, mongo_mp_table)).getConnect();
        userMongoCollection = new SearchMongodb(new MongoDB(mongo_hosts, mongo_user, mongo_passwd, mongo_db, mongo_user_table)).getConnect();
        //发送消息队列
        handle();

        //解析文件
        parserFile();
    }

    public static void parserFile() {
        String mpostPath = "";
        String rpostPath = "";
        String barPath = "";
        String userPath = "";

        if (localpath.endsWith("/")) {
            mpostPath = localpath + mpost_dir;
            rpostPath = localpath + rpost_dir;
            barPath = localpath + bar_dir;
            userPath = localpath + user_dir;
        } else {
            mpostPath = localpath + "/" + mpost_dir;
            rpostPath = localpath + "/" + rpost_dir;
            barPath = localpath + "/" + bar_dir;
            userPath = localpath + "/" + user_dir;
        }

        //主贴
        Thread mpostThread = new Thread(new ParserFileHandler(mpostPath, mpostQueue));
        mpostThread.setName("###Parser_mpost");
        mpostThread.start();

        //跟帖
        Thread rpostThread = new Thread(new ParserFileHandler(rpostPath, rpostQueue));
        rpostThread.setName("###Parser_rpost");
        rpostThread.start();

        //吧数据
        Thread barThread = new Thread(new ParserFileHandler(barPath, barQueue));
        barThread.setName("###Parser_bar");
        barThread.start();

        //用户
        Thread userThread = new Thread(new ParserFileHandler(userPath, userQueue));
        userThread.setName("###Parser_user");
        userThread.start();
    }

    public static void handle() {
        //主贴
        MPostHandler mpostHandler = new MPostHandler();
        Thread mpostThread = new Thread(mpostHandler);
        mpostThread.setName("##MPOST##");
        mpostThread.start();

        //跟帖
        RPostHandler rpostHandler = new RPostHandler();
        Thread rpostThread = new Thread(rpostHandler);
        rpostThread.setName("##RPost##");
        rpostThread.start();

        //吧数据
        BarHandler barHandler = new BarHandler();
        Thread barThread = new Thread(barHandler);
        barThread.setName("##Bar##");
        barThread.start();

        //用户
        UserHandler userHandler = new UserHandler();
        Thread userThread = new Thread(userHandler);
        userThread.setName("##User##");
        userThread.start();
    }

}
